package com.smart.primary.util.creat;

import java.io.PrintStream;
import java.lang.reflect.Field;
import java.util.List;

public class CreatMapper {

	public static void creat(List<Class<?>> cla) {
		try {
			PrintStream ps;
			for (Class<?> cc : cla) {
				ps = new PrintStream(Creat.getPath(Creat.mapper)+cc.getSimpleName()+Creat.mapper+".xml");
				System.setOut(ps);//把创建的打印输出流赋给系统。即系统下次向 ps输出
				getMapper1(cc);//输出的内容
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void getMapper(Class<?> cc) {
		List<Field> field = Creat.getFieldList(cc);
		String str = cc.getName();
		str = StrUtil.subIndOf(str,".",-2);
		str += "."+StrUtil.toLowerOne(Creat.dao)+"."+cc.getSimpleName()+Creat.dao;
	    System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n" + 
	    		"<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >\r\n" + 
	    		"<mapper namespace=\""+str+"\" >");
/**
 * sql 片段  所有的字段
 *  
 */																		 
	    System.out.println("<!-- "+cc.getSimpleName()+"Mapper -->\r\n<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ziduan--> ");
	    System.out.println("<sql id=\"columnList\">");
	    for (Field fe : field) {
	    	if(fe==field.get(field.size()-1)) {
	    		System.out.println("a."+fe.getName());
	    	}else {
	    		System.out.print("a."+fe.getName()+",");
	    	}
		}	
	    
	    System.out.println("</sql>");	    	    
/**
 * sql 片段 查询 条件	    																								  
 */
		System.out.println("<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++select-->");
	    System.out.println("<sql id=\"queryBy\">");
	    for (Field fe : field) {
	    	 //String 类型
			if("String".equals(fe.getType().getSimpleName())) {
				System.out.println("<if test=\""+fe.getName()+" != null and "+fe.getName()+" !=''\">");
				System.out.println("	and a."+fe.getName()+" = '#{"+fe.getName()+"}'");
				System.out.println("</if>");
			}else {
				System.out.println("<if test=\""+fe.getName()+" != null \">");
				System.out.println("	and a."+fe.getName()+" = #{"+fe.getName()+"}");
				System.out.println("</if>");
			}
	    }
//	    for (Field fe : field) {
//		    if(!"String".equals(fe.getType().getSimpleName())){
//		    	if("java.lang.Boolean".equals(fe.getType().getName()) || fe.getName().toLowerCase().endsWith("id")) {
//		    		System.out.println("<if test=\""+fe.getName()+" != null and "+fe.getName()+" != -1 \">");
//		    		System.out.println("	and a."+fe.getName()+" = #{"+fe.getName()+"}");
//					System.out.println("</if>");
//		    	}else {
//		    		System.out.println("<if test=\""+fe.getName()+" != null \">");
//					System.out.println("	and a."+fe.getName()+" = #{"+fe.getName()+"}");
//					System.out.println("</if>");
//		    	}
//			}
//	    }
	    /**
	     * 实体类继承了分页基类后才能用
	     */
//	    System.out.println("<if test=\"stringId != null and stringId !=''\">\r\n	and a."+field[0].getName()+" in (${stringId})\r\n</if>\r\n</sql>");
/**
 *  按条件查询集合	    
 */	
	    System.out.println("<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++list-->");
	    System.out.println("<select id=\"getList\" resultType=\""+cc.getName()+"\">");
	    System.out.println("	select");
	    System.out.println("<include refid=\"columnList\" />");
	    System.out.println("	from "+StrUtil.toLowerOne(cc.getSimpleName())+" a where 1=1");
	    System.out.println("<include refid=\"queryBy\"/>");
	    System.out.println("<if test=\"sort != null and sort != '' and sortVal != null\">\r\n" +
	    		"	<if test=\"sortVal == 1\">\r\n		order by ${sort} asc\r\n	</if>\r\n"+
	    		"	<if test=\"sortVal == 0\">\r\n		order by ${sort} desc\r\n	</if>\r\n</if>\r\n" +
	    		"<if test=\"rows !=null and index != null\">\r\n	limit #{index},#{rows}\r\n</if>\r\n</select>");
/**
 * 条件查询长度	    
 */	
//	    System.out.println("<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++count-->");
//	    System.out.println("<select id=\"get"+cc.getSimpleName()+"Count\" resultType=\"int\">");
//	    System.out.println("	select count(a."+field[0].getName()+") from "+toLowerLine(cc.getSimpleName())+" a where 1=1");
//	    System.out.println("<include refid=\"queryBy\" />\r\n</select>");
/**
 * 根据id获取一个	    
 */
//	    System.out.println("<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++one-->");
//	    System.out.println("<select id=\"get"+cc.getSimpleName()+"One\" parameterType=\"java.lang.Integer\" resultType=\""+cc.getName()+"\">");
//	    System.out.println("	select \r\n<include refid=\"columnList\" />\r\n 	from "+toLowerLine(cc.getSimpleName())+" a where 1=1");
//	    System.out.println("<if test=\"_parameter != null \">\r\n 	and a."+field[0].getName()+"=#{_parameter}\r\n</if>\r\n</select>");
/**
 * 添加
 */		System.out.println("<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++add-->");
    	System.out.println("<insert id=\"add"+cc.getSimpleName()+"\">\r\n	insert into "+StrUtil.addStr(cc.getSimpleName()));
    	System.out.println("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\r\n");
    	for (Field fe : field) {
    		if("java.lang.Boolean".equals(fe.getType().getName()) || fe.getName().toLowerCase().endsWith("id")) {
    			System.out.println("<if test=\""+fe.getName()+" != null and "+fe.getName()+" != -1\">\r\n	"+fe.getName()+",\r\n</if>");
    		}else {
    			System.out.println("<if test=\""+fe.getName()+" != null\">\r\n	"+fe.getName()+",\r\n</if>");
    		}
		}
    	System.out.println("</trim>\r\n<trim prefix=\"values (\" suffix=\")\" suffixOverrides=\",\">"); 
    	for (Field fe : field) {
    		if("java.lang.Boolean".equals(fe.getType().getName()) || fe.getName().toLowerCase().endsWith("id")) {
    			System.out.println("<if test=\""+fe.getName()+" != null and "+fe.getName()+" != -1\">\r\n	#{"+fe.getName()+"},\r\n</if>");
    		}else {
    			System.out.println("<if test=\""+fe.getName()+" != null\">\r\n	#{"+fe.getName()+"},\r\n</if>");
    		}
		}
    	System.out.println("</trim>\r\n</insert>");
/**
 * 修改    	
 */
    	System.out.println("<!--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++update-->");
    	System.out.println("<update id=\"update"+cc.getSimpleName()+"ById\">\r\n	update "+StrUtil.addStr(cc.getSimpleName())+"\r\n<set>");
    	for (Field fe : field) {
    		if("java.lang.Boolean".equals(fe.getType().getName()) || fe.getName().toLowerCase().endsWith("id")) {
    			System.out.println("<if test=\""+fe.getName()+" != null and "+fe.getName()+" != -1\">\r\n	"+fe.getName()+"= #{"+fe.getName()+"},\r\n</if>");
    		}else {
    			System.out.println("<if test=\""+fe.getName()+" != null\">\r\n	"+fe.getName()+"= #{"+fe.getName()+"},\r\n</if>");
    		}
		}
    	System.out.println("</set>\r\n	where id in (${stringId})\r\n</update>");
/**
 * 删除
 */
//    	System.out.println("<!--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++delete-->");
//    	System.out.println("<delete id=\"del"+cc.getSimpleName()+"ById\">\r\n	delete from "+toLowerLine(cc.getSimpleName())+" where "+field[0].getName()+" in (${stringId})\r\n</delete>\r\n</mapper>");
	}

	public static  void getMapper1(Class<?> cc) {
        List<Field> field = Creat.getFieldList(cc);
        String str = cc.getName();
        str = StrUtil.subIndOf(str,".",-2);
        str += "."+StrUtil.toLowerOne(Creat.dao)+"."+cc.getSimpleName()+Creat.dao;
		System.out.println(
				"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
                "<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\r\n" +
                "<mapper namespace=\"com.smart.buss.dao."+cc.getSimpleName()+"Mapper\" >");
		/**
		 * 字段
		 */
		System.out.println(
		        "<!-- "+cc.getSimpleName()+" -->\r\n" +
                "\t<sql id=\"columnList\">");
		System.out.print("\t\t");
        for (Field fe : field) {
            if(fe==field.get(field.size()-1)) {
                System.out.println("a."+StrUtil.addStr(fe.getName()));
            }else {
                System.out.print("a."+StrUtil.addStr(fe.getName())+",");
            }
        }
        /**
         *  查询条件
         */
        System.out.println(
                "\t</sql>\r\n" +
                "\r\n" +
                "\t<sql id=\"queryBy\">");

        for (Field fe : field) {
			//String 类型
			if("String".equals(fe.getType().getSimpleName())) {
				System.out.println(
						"\t\t<if test=\""+fe.getName()+" != null and "+fe.getName()+" !=''\">\r\n" +
						"\t\t\tand a."+StrUtil.addStr(fe.getName())+" = #{"+fe.getName()+"}\r\n" +
						"\t\t</if>");
			}else {
				System.out.println(
						"\t\t<if test=\""+fe.getName()+" != null \">\r\n" +
						"\t\t\tand a."+StrUtil.addStr(fe.getName())+" = #{"+fe.getName()+"}\r\n" +
						"\t\t</if>");
			}
		}
        System.out.println("\t</sql>\n");
		System.out.println(
				"\t<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++list-->\r\n" +
				"\t<select id=\"queryList\" resultType=\"com.smart.buss.vo."+cc.getSimpleName()+"\" parameterType=\"com.smart.buss.vo."+cc.getSimpleName()+"\">\r\n" +
				"\t\tselect\r\n" +
				"\t\t<include refid=\"columnList\" />\r\n" +
				"\t\tfrom "+StrUtil.addStr(cc.getSimpleName())+" a where 1=1\r\n" +
				"\t\t<include refid=\"queryBy\"/>\r\n" +
				"\t</select>");

		System.out.println(
				"\t<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++list-->\r\n" +
				"\t<select id=\"queryListByPage\" resultType=\"com.smart.buss.vo."+cc.getSimpleName()+"\" parameterType=\"com.smart.buss.vo."+cc.getSimpleName()+"\">\r\n" +
				"\t\tselect\r\n" +
				"\t\t<include refid=\"columnList\" />\r\n" +
				"\t\tfrom "+StrUtil.addStr(cc.getSimpleName())+" a where 1=1\r\n" +
				"\t\t<include refid=\"queryBy\"/>\r\n" +
				"\t</select>");

		System.out.println(
				"\t<select id=\"queryCount\" resultType=\"java.lang.Integer\" parameterType=\"com.smart.buss.vo."+cc.getSimpleName()+"\">\r\n" +
				"\t\tselect\r\n" +
				"\t\tcount(*) ct\r\n" +
				"\t\tfrom "+StrUtil.addStr(cc.getSimpleName())+" a where 1=1\r\n" +
				"\t\t<include refid=\"queryBy\"/>\r\n" +
				"\t</select>");
		/**
		 * add
		 */
		System.out.println(
				"\t<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++add-->\r\n" +
				"\t<insert id=\"insert\">\r\n" +
				"\t\tinsert into "+StrUtil.addStr(cc.getSimpleName())+"\r\n" +
				"\t<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">");
		for (Field fe : field) {
			if("String".equals(fe.getType().getSimpleName())) {
				System.out.println("\t\t<if test=\""+fe.getName()+" != null and "+fe.getName()+" != ''\">\r\n\t\t\t"+StrUtil.addStr(fe.getName())+",\r\n\t\t</if>");
			}else {
				System.out.println("\t\t<if test=\""+fe.getName()+" != null\">\r\n\t\t\t"+StrUtil.addStr(fe.getName())+",\r\n\t\t</if>");
			}
		}
		System.out.println("\t</trim>\r\n\t<trim prefix=\"values (\" suffix=\")\" suffixOverrides=\",\">");
		for (Field fe : field) {
			if("String".equals(fe.getType().getSimpleName())) {
				System.out.println("\t\t<if test=\""+fe.getName()+" != null and "+fe.getName()+" != ''\">\r\n\t\t\t#{"+fe.getName()+"},\r\n\t\t</if>");
			}else {
				System.out.println("\t\t<if test=\""+fe.getName()+" != null\">\r\n\t\t\t#{"+fe.getName()+"},\r\n\t\t</if>");
			}
		}
		System.out.println("\t</trim>\r\n\t</insert>");

		/**
		 * update
		 */
		System.out.println(
				"\t<!--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++update-->\r\n" +
				"\t<update id=\"update\">\r\n" +
				"\t\tupdate "+StrUtil.addStr(cc.getSimpleName())+"\r\n" +
				"\t<set>");
		for (Field fe : field) {
			if("String".equals(fe.getType().getSimpleName())) {
				System.out.println("\t\t<if test=\""+fe.getName()+" != null and "+fe.getName()+" != ''\">\r\n\t\t\t"+StrUtil.addStr(fe.getName())+"= #{"+fe.getName()+"},\r\n\t\t</if>");
			}else {
				System.out.println("\t\t<if test=\""+fe.getName()+" != null\">\r\n\t\t\t"+StrUtil.addStr(fe.getName())+"= #{"+fe.getName()+"},\r\n\t\t</if>");
			}
		}
		System.out.println("\t</set>\r\n\t\twhere id = #{id}\r\n\t</update>");

		System.out.println(
				"\t<delete id=\"delete\" parameterType=\"java.lang.String\">\r\n" +
				"\t\tdelete from "+StrUtil.addStr(cc.getSimpleName())+" where id in\r\n" +
				"\t\t<foreach item=\"item\" collection=\"_parameter.split(',')\"  open=\"(\" separator=\",\" close=\")\">\r\n" +
				"\t\t\t'${item}'\r\n" +
				"\t\t</foreach>\r\n" +
				"\t</delete>");

		System.out.println("</mapper>");

	}



}
